<?php
class sas extends MY_Controller{

	function __construct(){
		parent::__construct();
		$this->config->load("raw");

		$this->validateSas();
	}
	
	function index(){
		$this->home();
//		echo "hellow";
	}
	
	function home(){
		$this->title = "Sas";
		$this->load->model('scoremod');
		
		/*
		 * load header
		 * @param load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewSas/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;
		
			
			$member = get_cookie('member');
			$score = $this->scoremod->get_where($member);
			$que = array('score'=>$score);
		
		$add['w_lpanel_content'] = "50%";
		$add['w_rpanel_content'] = "45%";
		$add["left"] = $this->load->view("sas/dashboard/left",$que,true);
		$add["right"] = $this->load->view("sas/dashboard/right","",true);
		
//			$c_lpanel['admin'] = $this->load->view("sas/lpanel/admin",'',true);
//			$c_lpanel['teacher'] = $this->load->view("sas/lpanel/teacher",'',true);
//			$c_lpanel['student'] = $this->load->view("sas/lpanel/student",'',true);
			$c_lpanel = array(
							'admin'=>$this->load->view("sas/lpanel/admin",'',true),
							'teacher'=>$this->load->view("sas/lpanel/teacher",'',true),
							'student'=>$this->load->view("sas/lpanel/student",'',true),
						);
		$content['lpanel_main'] = $this->load->view("sas/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("sas/dashboard/home",$add,true);
		$content = $this->load->view("finalizeViewSas/content",$content,true);
		
		/*
		 * load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("formcheck"));
		$css = css_script(array("nav","table","formcheck"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $this->title,
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function quiz(){
		$this->title = "Quiz";
		$this->load->model('radiomod');
		$this->load->model('subjectmod');
		
		/*
		 * load header
		 * @param load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewSas/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;
				
				/*
				 * get member
				 */
				$member = get_cookie('member');
				$qm = $this->db->query("select * from member where id_member='$member' limit 1");
				foreach ($qm->result() as $re):
						$username = $re->username;
				endforeach;
				
				/*
				 * get member student
				 */
				$qmst = $this->db->query("select * from member_student where id_member='$member' limit 1");
				foreach ($qmst->result() as $re):
						$year 		= $re->year;
						$faculty 	= $re->faculty;
						$class		= $re->class;
				endforeach;
				
				/*
				 * show list que for student
				 */
				$now = date('Y');
				$x_year = $now;
				$xi_year = ($now-1);
				$xii_year = ($now-2);
								
				if($year==$x_year){
					$year = "x";
				} else if($year==$xi_year){
					$year = "xi";
				} else {
					$year = "xii";
				}
			
//				echo $now." ".$x_year." ".$year;
				
								
				if($year=="x"){
//					$qsbjx = $this->db->query("select id_subject from subject where name='kkpi' limit 1");
//					foreach ($qsbjx->result() as $re):
//							$id_subject= $re->id_subject;
//					endforeach;
					
					$query = array(
								'query'=>$this->subjectmod->get_where('kkpi',0,1)
							);
					
				} else if($year=="xi"){
//					$qsbjxi = $this->db->query("select id_subject from subject where name='bahasa inggris' limit 1");
//					foreach ($qsbjxi->result() as $re):
//							$id_subject= $re->id_subject;
//					endforeach;

					$query = array(
								'query'=>$this->subjectmod->get_where('bahasa inggris',0,1)
							);
				}
				
				
				
				

			$c_lpanel = array(
							'admin'=>$this->load->view("sas/lpanel/admin",'',true),
							'teacher'=>$this->load->view("sas/lpanel/teacher",'',true),
							'student'=>$this->load->view("sas/lpanel/student",'',true),
						);
		$content['lpanel_main'] = $this->load->view("sas/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("sas/quiz/listque",$query,true);
		$content = $this->load->view("finalizeViewAdmin/content",$content,true);
		
		/*
		 * load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("formcheck"));
		$css = css_script(array("nav","table","formcheck"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $this->title,
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function ajax_score_show(){
		$this->load->model('scoremod');
		$id = $this->uri->segment(3,0);
		
		$query = $this->scoremod->get_record_unlimited();
		
		$this->load->view('sas/quiz/listscore',array('sc'=>$query));
		
	}
	
	function score_deact(){
		$this->load->model("scoremod");
		$id = $this->uri->segment(3,0);
		$data = array('status'=>"deactive");
		$this->scoremod->update($id,$data);
		return true;
	}
	
	function score_act(){
		$this->load->model("scoremod");
		$id = $this->uri->segment(3,0);
		$data = array('status'=>"active");
		$this->scoremod->update($id,$data);
		return true;
	}
	
	function score_corected(){
		$this->load->model("scoremod");
		$this->load->model("answermod");
		$this->load->model("radiomod");
		
		$id = $this->uri->segment(3,0);
		
		$subject = "";
		$session = "";
		$qsc = $this->db->query("select * from score where id_score='$id' limit 1");
		foreach ($qsc->result() as $re):
				$subject= $re->subject;
				$session= $re->session;
		endforeach;
		echo $session."<br />";
		
//		$qsc = $this->db->query("select * from answer where subject='$subject' and session='$session' and answer='true'");
//		foreach ($qsc->result() as $re):
//				$subject= $re->subject;
//				$session= $re->session;
//		endforeach;

		$radio = $this->radiomod->get_where_subject($subject);
		$radio = $radio->num_rows();
		
		$true = $this->answermod->get_where_subject_true($id,$session);
		$true = $true->num_rows();
		
		
		$t_count = (100/$radio);
		$count = $t_count*$true;
//		$count = character_limiter($count, 5);
		$count = round($count,2);
		echo $radio." | true : ".$true," | count : ".$count;
		
		
		
//		$data = array('score'=>"active");
//		$this->scoremod->update($id,$data);
		return true;
	}
	
	function score(){
		$this->title = "Quiz";
		$this->load->model('scoremod');
		$this->load->model('radiomod');
		$this->load->model('subjectmod');
		
		/*
		 * load header
		 * @param load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewSas/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;

			$c_lpanel = array(
							'admin'=>$this->load->view("sas/lpanel/admin",'',true),
							'teacher'=>$this->load->view("sas/lpanel/teacher",'',true),
							'student'=>$this->load->view("sas/lpanel/student",'',true),
						);
		$content['lpanel_main'] = $this->load->view("sas/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("sas/quiz/score",'',true);
		$content = $this->load->view("finalizeViewAdmin/content",$content,true);
		
		/*
		 * load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("formcheck","LightFace","LightFace.Request"));
		$css = css_script(array("nav","table","formcheck","LightFace"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $this->title,
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function play(){
		$this->title = "Quiz";
		$this->load->model('radiomod');
		$this->load->model('subjectmod');
		
		$id_subject = $this->uri->segment(3,0);
		$author = get_cookie('member');
		
		/*
		 * validate score
		 */
		$this->validateScore($author,$id_subject);
		
		/*
		 * load header
		 * @param load config header first
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("finalizeViewSas/header",$header,true);
		
		/*
		 * load content
		 * @note this content need width lpanel and width rpanel
		 * @param load config content first 
	
		 */
		$content = $this->config->item("content");
		$content['title'] = $this->title;
		$content['w_lpanel_main'] = "15%";
		$content['w_rpanel_main'] = "75%";
		
		$limit = 0;
		$offset = 100000000;
		
		
		$query = array('query'=>$this->radiomod->get_where_subject($id_subject));		

			$c_lpanel = array(
							'admin'=>$this->load->view("sas/lpanel/admin",'',true),
							'teacher'=>$this->load->view("sas/lpanel/teacher",'',true),
							'student'=>$this->load->view("sas/lpanel/student",'',true),
						);
		$content['lpanel_main'] = $this->load->view("sas/lpanel/lpanel",$c_lpanel,true);
		$content['rpanel_main'] = $this->load->view("sas/quiz/play",$query,true);
		$content = $this->load->view("finalizeViewAdmin/content",$content,true);
		
		/*
		 * load footer
		 * @param load config content first
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("finalizeViewAdmin/footer",$footer,true);
		
		/*
		 * finalize view 
		 * @optional : when need jscript or css ADD jscript and css with js_script() css_script() 
		 * @param config page 
		 * @finalizeView : array(title,jscript_add,css_add,jscript,css,header,content,footer);
		 */
		$jscript = js_script(array("jquery.min"));
		$css = css_script(array("nav","table"));
		$page = $this->config->item('page');
		$page['title'] = $this->title;
		$this->finalizeViewAdmin(array(
			'title' => $this->title,
			'jscript_add'=>$jscript,
			'css_add'=>$css,
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header' => $header,
			'content' => $content,
			'footer' => $footer
		));
	}
	
	function answer(){
		$this->load->model('answermod');
		$this->load->model('radiomod');
		$this->load->model('scoremod');
		
		$dses = date("H:m:s");
		$session = $this->answermod->generateSession($dses);
		$id_subject = $this->uri->segment(3,0);
		$author = get_cookie('member');
		
		/*
		 * validate score
		 */
		$this->validateScore($author,$id_subject);
		
		$opt_true = "";
		$querad = $this->db->query("select * from radio where id_subject='$id_subject'");
		foreach ($querad->result() as $re):
			$id_radio = $re->id_radio;
			$opt = $this->input->post($id_radio);
//			echo $id_radio." | ".$opt."<br>";
			
				$queopt = $this->db->query("select * from radio where id_radio='$id_radio'");
				foreach ($queopt->result() as $res):
					switch($res->opt){
						case "a" : $opt_true = $res->a;break;
						case "b" : $opt_true = $res->b;break;
						case "c" : $opt_true = $res->c;break;
						case "d" : $opt_true = $res->d;break;
						case "e" : $opt_true = $res->e;break;
					}
				endforeach;
				
//				echo $id_radio." | ".$opt." | ".$opt_true."<br>";
			if($opt==$opt_true){
				$optt = "true";
			} else {
				$optt = "false";
			}
			
			$data = array(
							'id_answer'=>'',
							'author'=>$author,
							'subject'=>$id_subject,
							'radio'=>$id_radio,
							'session'=>$session,
							'answer'=>$opt,
							'opt'=>$optt,
						);
				$this->answermod->add($data);
		endforeach;
		
//		$true = $this->db->query("select true from answer where session='$session' and author='$author'");
//		foreach ($true->result() as $re):
//			$true = $re->true;
//		endforeach;
		
//		$false = $this->db->query("select false from answer where session='$session' and author='$author'");
//		foreach ($true->result() as $re):
//			$false = $re->false;
//		endforeach;

		$true = $this->db->query("select true from answer where subject='$id_subject' and session='$session' and author='$author' and opt='true'");
		$false = $this->db->query("select false from answer where subject='$id_subject' and session='$session' and author='$author' and opt='false'");
		
		$count_radio = $this->radiomod->get_where_subject($id_subject);
		
		$count_radio = $count_radio->num_rows();
		$trues = $true->num_rows();
		$falses = $false->num_rows();
		
		
		
		$t_count = (100/$count_radio);
		$count = $t_count*$trues;
//		$count = character_limiter($count, 5);
		$count = round($count,2);
//		$count = floor($trues*100/count($falses));
		
		
		$score = array(
					'id_score'=>'',
					'author'=>$author,
					'subject'=>$id_subject,
					'session'=>$session,
					'score'=>$count,
				);
		
		$scr = "";
//		$val_score = $this->db->query("select score from score where subject='$id_subject'");
//		foreach ($val_score->result() as $re):
//			$scr = $re->score;
//		endforeach;
////		echo "db scr : ".$scr."<br>";
//		if($scr=="" || $scr==NULL){
			
			$this->scoremod->add($score);
//		}
		
//		echo $trues." / ".$count_radio."<br />";
//		echo $t_count." / ".$trues."<br />";
//		echo "score : ".$count."<br />";
//		echo "true : ".$trues."<br />";
//		echo "false : ".$falses;

		redirect("sas");
		return true;
	}
	
	function validateScore($member,$subject){
		$scr = "";
		$status = "";
		$val_score = $this->db->query("select score,status from score where subject='$subject' and author='$member'");
		foreach ($val_score->result() as $re):
			$scr = $re->score;
			$status = $re->status;
		endforeach;
//		echo "db scr : ".$scr."<br>";
//		echo $status;
		if($status="deactive" || $scr=="" || $scr==NULL){		
		} else {
			redirect('sas');
		}
	}
	
}